package cn.hrlly.tao2shou.callback;

import com.google.gson.Gson;
import com.zhy.http.okhttp.callback.Callback;

import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.domain.User;
import cn.hrlly.tao2shou.listener.OnFindPasswordListener;
import okhttp3.Request;
import okhttp3.Response;

/**
 * User: gengliang
 * Date: 2016-01-27
 * Time: 10:49
 */

public class FindPasswordCallback extends Callback<User> {

    private OnFindPasswordListener onFindPasswordListener;

    public FindPasswordCallback(OnFindPasswordListener onFindPasswordListener){
        this.onFindPasswordListener=onFindPasswordListener;
    }

    @Override
    public User parseNetworkResponse(Response response) throws Exception {
        String string=response.body().string();
        User user=new Gson().fromJson(string,User.class);
        return user;
    }

    @Override
    public void onError(Request request, Exception e) {
        onFindPasswordListener.findPasswordFail(R.string.network_connection_error);
    }

    @Override
    public void onResponse(User user) {
        if(user.getStatus()==0)
            onFindPasswordListener.findPasswordSucc(user);
        else
            onFindPasswordListener.findPasswordFail(user.getErrorMessage());
    }
}
